package com.everest;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;

public class Tank {
	int x,y;
	public static final int SPEED = 5;
	public static final int WIDTH = 30;
	public static final int HEIGHT = 30;
	
	boolean bL=false,bU=false,bR=false,bD=false;
	Direction dir ;
	TankClient tc;
	Direction ptDir = Direction.D;
	
	public static int deadTimes = 0;
	private boolean good;
	private boolean live = true;
	public static Random r = new Random();
	private int step = r.nextInt(12)+3;
	private boolean released = true;
	private boolean key_S_Resesed = true;
	private int oldX,oldY;
	private int life;
	private BloodBar bb = new BloodBar();
	
	private static Map<String,Image> imgs = new HashMap<String,Image>();
	private static Toolkit tk = Toolkit.getDefaultToolkit();
	private static Image[] tankImages = null;
	
	static {
		tankImages = new Image[] {
				tk.getImage(Explode.class.getClassLoader().getResource("images/tankL.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/tankLU.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/tankU.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/tankRU.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/tankR.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/tankRD.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/tankD.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/tankLD.gif")),
		   };
		imgs.put("L", tankImages[0]);
		imgs.put("LU", tankImages[1]);
		imgs.put("U", tankImages[2]);
		imgs.put("RU", tankImages[3]);
		imgs.put("R", tankImages[4]);
		imgs.put("RD", tankImages[5]);
		imgs.put("D", tankImages[6]);
		imgs.put("LD", tankImages[7]);
		
	}
	
	public Tank(int x, int y, boolean good) {
		this.x = x;
		this.y = y;
		this.oldX = x;
		this.oldY = y;
		this.good = good;
		
		life = Integer.parseInt(PropertyMgr.getProperty("initTankLife"));
	}
	
	public Tank(int x,int y,boolean good,Direction dir,TankClient tc) {
		this(x,y,good);
		this.tc = tc;
		this.dir = dir;
	}
	
	public void draw(Graphics g) {
		if(!live) {
			if(!good){
				tc.enemyTanks.remove(this);
			}
			return;
		}
		
		/*Color c = g.getColor();
		if(good) g.setColor(Color.red);
		else g.setColor(Color.BLUE);
		//g.drawOval(30, 30, 10, 10);
		g.fillOval(x, y, WIDTH, HEIGHT);
		g.setColor(c);
		*/
		//locateDirection();
		if(good) bb.draw(g);
		switch(ptDir) {
		case L:
			g.drawImage(imgs.get("L"), x,y, null);
			break;
		case LU:
			g.drawImage(imgs.get("LU"), x,y, null);
			break;
		case U:
			g.drawImage(imgs.get("U"), x,y, null);
			break;
		case RU:
			g.drawImage(imgs.get("RU"), x,y, null);
			break;
		case R:
			g.drawImage(imgs.get("R"), x,y, null);
			break;
		case RD:
			g.drawImage(imgs.get("RD"), x,y, null);
			break;
		case D:
			g.drawImage(imgs.get("D"), x,y, null);
			break;
		case LD:
			g.drawImage(imgs.get("LD"), x,y, null);
			break;
	}
		move();
	}
	
	public void locateDirection() {
		if(bL && !bU && !bR && !bD) dir = Direction.L;
		else if(bL && bU && !bR && !bD) dir = Direction.LU;
		else if(!bL && bU && !bR && !bD) dir = Direction.U;
		else if(!bL && bU && bR && !bD) dir = Direction.RU;
		else if(!bL && !bU && bR && !bD) dir = Direction.R;
		else if(!bL && !bU && bR && bD) dir = Direction.RD;
		else if(!bL && !bU && !bR && bD) dir = Direction.D;
		else if(bL && !bU && !bR && bD) dir = Direction.LD;
		else if(!bL && !bU && !bR && !bD) dir = Direction.STOP;
		
	}
 	
	public void keyPress(KeyEvent e){ 
		
		int keyCode = e.getKeyCode();
		switch(keyCode) {
		case 37:
			bL = true;
			break;
		case 38:
			bU = true;
			
			break;
		case 39:
			bR = true;
			
			break;
		case 40:
			bD = true;
			break;
		case 32:
			if(live && released) fire(this.good);
			this.released = false;
			break;
		case 67:
			tc.constractTanks();
			break;
		case KeyEvent.VK_A:
			if(!tc.myTank.live) {
				tc.myTank.live = true;
				tc.myTank.setLife(100);
			}
			break;
		case KeyEvent.VK_S :
			if(key_S_Resesed) superFire();
			key_S_Resesed = false;
			break;
			default:
		}
		locateDirection();
		//System.out.println(keyCode);
		//repaint();
	}
	
	public void keyReleased(KeyEvent e){ 
		
		int keyCode = e.getKeyCode();
		switch(keyCode) {
		case 37:
			bL = false;
			break;
		case 38:
			bU = false;
			
			break;
		case 39:
			bR = false;
			
			break;
		case 40:
			bD = false;
			break;
		case 32:
			this.released = true;
			break;
		case KeyEvent.VK_S :
			key_S_Resesed = true;
			break;
			default:
		}
		locateDirection();
		System.out.println(keyCode);
		//repaint();
	}
	
	public Missile fire(boolean good) {
		int x = this.x+Tank.WIDTH/2-Missile.WIDTH/2;
		int y = this.y+Tank.HEIGHT/2-Missile.HEIGHT/2;
		Missile m = new Missile(x,y,ptDir,this.tc,good);
		tc.missiles.add(m);
		return m;
	}
	
	public Missile fire(boolean good,Direction dir) {
		int x = this.x+Tank.WIDTH/2-Missile.WIDTH/2;
		int y = this.y+Tank.HEIGHT/2-Missile.HEIGHT/2;
		Missile m = new Missile(x,y,dir,this.tc,good);
		tc.missiles.add(m);
		return m;
	}
	
	public void move()  {
		
		oldX = x;
		oldY = y;
		if(!good) {
			if(step == 0){
				fire(this.good);
				step = r.nextInt(12)+3;
				Direction[] dirs = Direction.values();
				int rn = r.nextInt(dirs.length);
				dir = dirs[rn];
			}
			step--;
			//if(r.nextInt(40) > 30) fire(this.good);
		}
		
		switch(dir) {
		case L:
			x -= SPEED;	
			break;
		case LU:
			x -= SPEED;
			y -= SPEED;
			break;
		case U:
			y -= SPEED;
			break;
		case RU:
			y -= SPEED;
			x += SPEED;
			break;
		case R:
			x += SPEED;
			break;
		case RD:
			x += SPEED;
			y += SPEED;
			break;
		case D:
			y += SPEED;
			break;
		case LD:
			y += SPEED;
			x -= SPEED;
			break;
		case STOP:
			break;
		}
		if(dir != Direction.STOP){
			ptDir= this.dir;
		}
		if(x < 6) x = 6;
		if(x > TankClient.GAME_WIDTH-Tank.WIDTH-10) x = TankClient.GAME_WIDTH-Tank.WIDTH-10;
		if(y < 30) y = 30;
		if(y > TankClient.GAME_HEIGHT-Tank.HEIGHT-10) y = TankClient.GAME_HEIGHT-Tank.HEIGHT-10;
	}
	
	
	public Rectangle getRect() {
		return new Rectangle (x,y,WIDTH,HEIGHT);
	}

	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}

	public boolean isGood() {
		return good;
	}
	
	private void stay() {
		x = oldX;
		y = oldY;
	}
	
	public boolean hitWall(Wall w) {
		if(this.live && this.getRect().intersects(w.getRect())) {
			stay();
			return true;
		}
		return false;
	}
	
	public boolean collidesWithTank(java.util.List<Tank> enemyTanks) {
		for(int i=0; i<tc.enemyTanks.size();i++) {
			Tank t = tc.enemyTanks.get(i);
			if(this != t) {
				if(this.live && t.isLive() && this.getRect().intersects(t.getRect())) {
					this.stay();
					t.stay();
					return true;
				}
			}
		}
		
		return false;
	}
	
	public void superFire() {
		Direction[] dirs = Direction.values();
		if(this.live) {
			for(int i=0; i<8;i++) {
				fire(this.good,dirs[i]);
			}
		}
	}

	public int getLife() {
		return life;
	}

	public void setLife(int life) {
		this.life = life;
	}
	
	public class BloodBar {
		public void draw(Graphics g) {
			Color c = g.getColor();
			g.setColor(Color.RED);
			g.drawRect(x-1, y-10, WIDTH, 10);
			g.fillRect(x-1, y-10, WIDTH*life/100, 10);
			g.setColor(c);
		}
	}
	
	
	public boolean eatBlood(Blood b) {
		if(this.live && b.isLive() && this.getRect().intersects(b.getRect())) {
			tc.b.setLive(false);
			tc.myTank.setLife(100);
			Blood.r = Tank.r.nextInt(20)+1;
			return true;
		}
		return false;
	}
}

